package my.sso.my_sso_demo01.controller;

import my.sso.my_sso_demo01.util.CookieUtil;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    private static final String USER_KEY = "user_key";

    @RequestMapping("/login")
    public Object login(String username, String password, HttpServletRequest request, HttpServletResponse response){
        Map<String,String>  map = new HashMap<>();
        map.put("user",username);
        map.put("pwd",password);

        //生成Cookie
        String s = CookieUtil.setLoginCookie(request,response);
        //Cookie为redis的key,map为value  ,存入redis
        stringRedisTemplate.opsForValue().set(s,map.toString());
        return  "成功了";
    }

    @RequestMapping("/getUser")
    public Object getUser(HttpServletRequest request,HttpServletResponse response){
        //通过cookie去拿用户登录信息
        String s = stringRedisTemplate.opsForValue().get(CookieUtil.getLoginCookie(request,response));
        return s;
    }

}
